/*
 * Copyright @ 2024 weili23
 * com.example.mythread.a10threadExerciseLottery 10:19
 * All right reserved.
 */

package com.example.mythread.a10threadExerciseLottery;

import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @desc:
 * @author: weili23
 * @createTime: 2024/10/22 10:19
 * @version: v1.0
 */
public class Demo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        /*
        *有一个抽奖池,该抽奖池中存放了奖励的金额,该抽奖池中的奖项为
        * {10,5,20,50,100,200,500,800,2,80,300,700}
        * 创建两个抽奖箱(线程)设置线程名称分别为“抽奖箱1“抽奖箱2
        * 随机从抽奖池中获取奖项元素并打印在控制台上,格式如下:
        * 每次抽出一个奖项就打印一个(随机)
            抽奖箱1 又产生了一个 10 元大奖
            抽奖箱1 又产生了一个 100 元大奖
            抽奖箱1 又产生了一个 200 元大奖
            抽奖箱1 又产生了一个 800 元大奖
            抽奖箱2 又产生了一个 700 元大奖
         */
        ArrayList<Integer> list = new ArrayList<>();
        Collections.addAll(list,10,5,20,50,100,200,500,800,2,80,300,700);
        /*
        第1种
         */
        /*MyLottery myLottery1 = new MyLottery(list);
        MyLottery myLottery2 = new MyLottery(list);
        myLottery1.setName("抽奖箱1");
        myLottery2.setName("抽奖箱2");
        myLottery1.start();
        myLottery2.start();*/
        /*
        第2种
         */
        /*MyLottery1 myLottery1 = new MyLottery1(list);
        MyLottery1 myLottery2 = new MyLottery1(list);
        myLottery1.setName("抽奖箱1");
        myLottery2.setName("抽奖箱2");
        myLottery1.start();
        myLottery2.start();*/
        /*
        第2种
         */
        MyLottery2 myLottery1 = new MyLottery2(list);
        MyLottery2 myLottery2 = new MyLottery2(list);
        FutureTask<Integer> futureTask1 = new FutureTask<>(myLottery1);
        FutureTask<Integer> futureTask2 = new FutureTask<>(myLottery2);
        Thread thread1 =new Thread(futureTask1);
        Thread thread2 =new Thread(futureTask2);
        thread1.setName("抽奖箱1");
        thread2.setName("抽奖箱2");
        thread1.start();
        thread2.start();
        Integer integer = futureTask1.get();
        Integer integer1 = futureTask2.get();
        System.out.println("抽奖箱1又产生了一个"+integer+"元大奖");
        System.out.println("抽奖箱2又产生了一个"+integer1+"元大奖");

    }
}
